package com.zwhu.nt;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * 窗体类 可视化界面
 * @version 2
 * @author ryan
 *
 */
public class AddressListSystem extends JFrame{
	/**标题标签 */
	JLabel title= new JLabel("通讯录");
	/**编号标签 */
	JLabel num= new JLabel("编号");
	/**姓名标签 */
	JLabel name= new JLabel("姓名");
	/**邮编标签 */
	JLabel zip= new JLabel("邮编");
	/**地址标签 */
	JLabel address= new JLabel("地址");
	/**电话标签 */
	JLabel telephone= new JLabel("电话");
	/**性别标签 */
	JLabel mobile= new JLabel("性别");
	/**邮箱标签 */
	JLabel email= new JLabel("邮箱");
	/**性别选择的数组 */
	String str1[] = {"","男","女"};
	/**性别选项框 */
	JComboBox jc=new JComboBox(str1);
	/**编号输入框 */
	JTextField jtxtnum= new JTextField();
	/**姓名输入框 */
	JTextField jtxtname = new JTextField();
	/**邮编输入框 */
	JTextField jtxtzip = new JTextField();
	/**地址输入框 */
	JTextField jtxtadd = new JTextField();
	/**电话输入框 */
	JTextField jtxttel = new JTextField();
	/**email输入框 */
	JTextField jtxtemail = new JTextField();
	
	/**显示按钮 */
	JButton jbtleap=new JButton("显示");
	/**添加按钮 */
	JButton jbtadd=new JButton("添加");
	/**查找按钮 */
	JButton jbtfind=new JButton("查找");
	/**删除按钮 */
	JButton jbtclear=new JButton("删除");
	/**修改按钮 */
	JButton jbtedit=new JButton("修改");
	/**退出按钮 */
	JButton jbtexit=new JButton("退出");

	/**Arraylist对象  御用获取文件信息 */
	ArrayList<Note> al=new ArrayList();

	/**
	 * 界面函数
	 * @param s
	 */
	public AddressListSystem(String s) {
		// TODO Auto-generated constructor stub
		super(s);
		
		Container cp=getContentPane();
		cp.setLayout(null);
		title.setBounds(100, 0, 300, 60);
		num.setBounds(50, 50, 75, 25);
		name.setBounds(50, 100, 75, 25);
		zip.setBounds(50, 150, 75, 25);
		address.setBounds(50, 200, 75, 25);
		telephone.setBounds(50, 250, 75, 25);
		mobile.setBounds(50, 300, 75, 25);
		email.setBounds(50, 350, 75, 25);
		jtxtnum.setBounds(150, 50, 100, 25);
		jtxtname.setBounds(150,100,100,25);
		jtxtzip.setBounds(150,150,100,25);
		jtxtadd.setBounds(150,200,250,25);
		jtxttel.setBounds(150,250,150,25);
		jtxtemail.setBounds(150,350,200,25);
		jc.setBounds(150, 300, 100, 25);
		jbtadd.setBounds(55,400,60,25);
		jbtfind.setBounds(125,400,60,25);
		jbtclear.setBounds(195,400,60,25);
		jbtexit.setBounds(350,400,60,25);
		jbtedit.setBounds(270,400,60,25);
		jbtleap.setBounds(200,15, 60, 25);

		/**
		 * 添加信息按钮
		 * 
		  */
		jbtadd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(jtxtname.getText().equalsIgnoreCase("")&&jtxtzip.getText().equalsIgnoreCase("")&&
					jtxtadd.getText().equalsIgnoreCase("")&&jtxttel.getText().equalsIgnoreCase("")&&
					jtxtemail.getText().equalsIgnoreCase("")&&jc.getSelectedItem().equals("")){
					JOptionPane.showMessageDialog(null,"记录为空","错误提示",
					JOptionPane.INFORMATION_MESSAGE);
					jtxtnum.setText("");
					jtxtname.setText("");
					jtxtzip.setText("");
					jtxtadd.setText("");
					jtxttel.setText("");
					jc.setSelectedItem("");
					jtxtemail.setText("");
					return;
				}
				
				try {
					ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
					al=(ArrayList)in.readObject();
					in.close();
				} catch (Exception ex) {
					// TODO: handle exception
					System.out.println("添加信息时文件读取错误");
				}
				
				try {
					ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.dat",false));
					Note temp= new Note();
					int i;
					for(i=0;i<al.size();i++){
						temp=(Note)al.get(i);
						if(temp.name.equalsIgnoreCase(jtxtname.getText())||
						   temp.num.equalsIgnoreCase(jtxtnum.getText())){
							break;
						}
						else{
							if(temp.telephone.equalsIgnoreCase(jtxttel.getText())&&
							  (jtxttel.getText().isEmpty()==false)){
								break;
							}
						}
					}
				
					if(!al.isEmpty()&&i!=al.size()){
						JOptionPane.showMessageDialog(null, "已经有此人或者编号占用或者电话已经存在,请更换",
						"错误提示",JOptionPane.INFORMATION_MESSAGE);
					}
					else{
						Note note=new Note();
						if(jtxtnum.getText().trim().equals("")){
							String t=null;
							if(al.size()==0){
								t=String.valueOf(1);
							}
							else{
								Note a=(Note)al.get(al.size()-1);
								int w=Integer.parseInt(a.num)+1;
								t=String.valueOf(w);
								System.out.println(w);
							}
						
							for(i=0;i<al.size();i++){
								temp=(Note)al.get(i);
								if(temp.name.equalsIgnoreCase(jtxtname.getText())||
									temp.num.equalsIgnoreCase(t)){
									break;
								}
							}

							if(!al.isEmpty()&&i!=al.size()){
								JOptionPane.showMessageDialog(null, "这个编号已被占用","错误提示",
								JOptionPane.INFORMATION_MESSAGE);
							}
							else{
								note.num=t;
							}
						}
						
						note.name=jtxtname.getText();
						note.zip=jtxtzip.getText();
						note.address=jtxtadd.getText();
						note.telephone=jtxttel.getText();
						note.sex=jc.getSelectedItem().toString();
						note.email=jtxtemail.getText();	
						al.add(note);
						out.writeObject(al);
					}
				} catch (Exception e2) {
					// TODO: handle exception
					System.out.println("文件编写有误");
				}				
				jtxtnum.setText("");
				jtxtnum.setText("");
				jtxtname.setText("");
				jtxtzip.setText("");
				jtxtadd.setText("");
				jtxttel.setText("");
				jc.setSelectedItem("");
				jtxtemail.setText("");
			}
		});
		
		/**
		 * 查询信息按钮
		 */
		jbtfind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
					al=(ArrayList)in.readObject();
					in.close();
				} catch (Exception e2) {
					// TODO: handle exception
					System.out.println("查询时读取文件出错");
				}
		
				if(al.isEmpty()){
					JOptionPane.showMessageDialog(null,"您的通讯录空空如也","错误提示",
					JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					if(jtxtname.getText().trim().equals("")&&jtxtnum.getText().trim().equals("")&&
					jtxttel.getText().trim().equals("")){
						int i;
						Note temp=new Note();
						ArrayList bl=new ArrayList();
						for(i=0;i<al.size();i++){
							temp=(Note)al.get(i);
							if(temp.sex.equals(jc.getSelectedItem().toString())){
								bl.add(temp);						
							}
						}

						if(!bl.isEmpty()){
							frame3 f2=new frame3(bl);
							f2.addWindowListener(new WindowAdapter() {
								public void windowClosing(WindowEvent e){
									setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
								}
							});
							f2.setSize(600,450);
							f2.setVisible(true);
						}
						else{
							jtxtname.setText("");
							jtxtzip.setText("");
							jtxtadd.setText("");
							jtxttel.setText("");
							jc.setSelectedItem("");
							jtxtemail.setText("");
							JOptionPane.showMessageDialog(null,"此性别的人类不存在于你的通讯录中","错误提示",
							JOptionPane.INFORMATION_MESSAGE);
						}
						System.out.println("通讯录中共有 "+al.size()+" 个人");
						for(int k=0;k<al.size();k++){
						temp=(Note)al.get(k);
						System.out.println("这个人的名字是: "+temp.name+",邮编是: "+
						temp.zip+",地址是: "+temp.address+",电话是: "+temp.telephone+
						",性别是: "+temp.sex+",电子邮箱是: "+temp.email);
						}	
						bl.clear();
					}
					else{
						int i;
						if(jtxtname.getText().trim().equals("")&&jtxttel.getText().trim().equals("")){
						Note temp=new Note();
						for(i=0;i<al.size();i++){
							temp=(Note)al.get(i);
							if(temp.num.equalsIgnoreCase(jtxtnum.getText()))
								break;
						}
						if(!al.isEmpty()&&i!=al.size()){
							jtxtname.setText(temp.name);
							jtxtzip.setText(temp.zip);
							jtxtadd.setText(temp.address);
							jtxttel.setText(temp.telephone);
							jc.setSelectedItem(temp.sex);
							jtxtemail.setText(temp.email);
						}
						else{
							jtxtnum.setText("");
							jtxtname.setText("");
							jtxtzip.setText("");
							jtxtadd.setText("");
							jtxttel.setText("");
							jc.setSelectedItem("");
							jtxtemail.setText("");
							JOptionPane.showMessageDialog(null,"无此人信息","错误提示",
							JOptionPane.INFORMATION_MESSAGE);
						}
						}
						else if(jtxttel.getText().trim().equals("")){
						Note temp=new Note();
						for(i=0;i<al.size();i++){
							temp=(Note)al.get(i);
							if(temp.name.equalsIgnoreCase(jtxtname.getText()))
								break;
						}
						if(!al.isEmpty()&&i!=al.size()){
							jtxtnum.setText(temp.num);
							jtxtzip.setText(temp.zip);
							jtxtadd.setText(temp.address);
							jtxttel.setText(temp.telephone);
							jc.setSelectedItem(temp.sex);
							jtxtemail.setText(temp.email);
						}
						else{
							jtxtnum.setText("");
							jtxtname.setText("");
							jtxtzip.setText("");
							jtxtadd.setText("");
							jtxttel.setText("");
							jc.setSelectedItem("");
							jtxtemail.setText("");
							JOptionPane.showMessageDialog(null,"无此人信息","错误提示",
							JOptionPane.INFORMATION_MESSAGE);
						}	
						System.out.println("通讯录中共有 "+al.size()+" 个人");
						for(int k=0;k<al.size();k++){
						temp=(Note)al.get(k);
						System.out.println("这个人的名字是: "+temp.name+",邮编是: "+temp.zip+",地址是: "+
						temp.address+",电话是: "+temp.telephone+",性别是: "+temp.sex+",电子邮箱是: "+temp.email);
						}	
					}
					
					else{
						Note temp=new Note();
						for(i=0;i<al.size();i++){
							temp=(Note)al.get(i);
							if(temp.telephone.equalsIgnoreCase(jtxttel.getText()))
								break;
						}	
						if(!al.isEmpty()&&i!=al.size()){
							jtxtnum.setText(temp.num);
							jtxtzip.setText(temp.zip);
							jtxtadd.setText(temp.address);
							jtxtname.setText(temp.name);
							jc.setSelectedItem(temp.sex);
							jtxtemail.setText(temp.email);
						}
						else{
							jtxtnum.setText("");
							jtxtname.setText("");
							jtxtzip.setText("");
							jtxtadd.setText("");
							jtxttel.setText("");
							jc.setSelectedItem("");
							jtxtemail.setText("");
							JOptionPane.showMessageDialog(null,"无此人信息","错误提示",
							JOptionPane.INFORMATION_MESSAGE);
							}
							System.out.println("通讯录中共有 "+al.size()+" 个人");
							for(int k=0;k<al.size();k++){
								temp=(Note)al.get(k);
								System.out.println("这个人的名字是: "+temp.name+",邮编是: "+
										temp.zip+",地址是: "+temp.address+",电话是: "+temp.telephone+
										",性别是: "+temp.sex+",电子邮箱是: "+temp.email);
							}
						}
					}
				}
			}
		});
		
		/**
		 * 删除信息按钮
		 */
		jbtclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
					al=(ArrayList)in.readObject();
					in.close();
				} catch (Exception e2) {
					// TODO: handle exception
					System.out.println("查询时读取文件出错");
				}
				
				Note temp=new Note();
				int i;
				for(i=0;i<al.size();i++){
					temp=(Note)al.get(i);
					if(temp.name.equalsIgnoreCase(jtxtname.getText()))
						break;
				}

				if(!al.isEmpty()&&i!=al.size()){
					jtxtnum.setText(temp.num);
					jtxtzip.setText(temp.zip);
					jtxtadd.setText(temp.address);
					jtxttel.setText(temp.telephone);
					jc.setSelectedItem(temp.sex);
					jtxtemail.setText(temp.email);
				}
				else{
					jtxtname.setText("");
					jtxtzip.setText("");
					jtxtadd.setText("");
					jtxttel.setText("");
					jc.setSelectedItem("");
					jtxtemail.setText("");
					JOptionPane.showMessageDialog(null,"无此人信息","错误提示",
					JOptionPane.INFORMATION_MESSAGE);
				}
				
				al.remove(i);
				jtxtnum.setText("");
				jtxtname.setText("");
				jtxtzip.setText("");
				jtxtadd.setText("");
				jtxttel.setText("");
				jc.setSelectedItem("");
				jtxtemail.setText("");

				try {
					ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.dat",false));
					out.writeObject(al);
				} catch (Exception e2) {
					// TODO: handle exception
				}
				JOptionPane.showMessageDialog(null, "删除成功","提示",JOptionPane.INFORMATION_MESSAGE);
			}
		});
		/**
		 * 编辑按钮
		 */
		jbtedit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Note note=new Note();
				note.num=jtxtnum.getText();
				note.name=jtxtname.getText();
				note.zip=jtxtzip.getText();
				note.address=jtxtadd.getText();
				note.telephone=jtxttel.getText();
				note.sex=jc.getSelectedItem().toString();
				note.email=jtxtemail.getText();
				int i =Integer.parseInt(note.num)-1;
				try {
					ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
					al=(ArrayList)in.readObject();
					in.close();
				} catch (Exception e2) {
					// TODO: handle exception
					System.out.println("查询时读取文件出错");
				}	

				ArrayList bl=new ArrayList();
				try {
					ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
					bl=(ArrayList)in.readObject();
					in.close();
				} catch (Exception e2) {
					// TODO: handle exception
					System.out.println("查询时读取文件出错");
				}	
				int j,h;
				int k=0;
				Note y=new Note();
				for(j=0;j<bl.size();j++){
					 y=(Note)bl.get(j);
					 if(y.num.trim().equals(note.num)){
						 bl.set(j, note);
						 break;
					 }
				}
				
				for(h=0;h<bl.size();h++){
					y=(Note)bl.get(h);
					if(y.name.equalsIgnoreCase(note.name)||y.num.equalsIgnoreCase(note.num)){
						k++;
					}
					else{
						if(y.telephone.equalsIgnoreCase(note.telephone)
						&&(jtxttel.getText().isEmpty()==false)){
							k++;
						}
					}
				}
				
				if(k>=2){
					JOptionPane.showMessageDialog(null, "更改失败因为存在重复","提示",
					JOptionPane.INFORMATION_MESSAGE);
					
				}
				else{
					al.set(j, note);
					JOptionPane.showMessageDialog(null, "更改成功","提示",JOptionPane.INFORMATION_MESSAGE);
					
				}
	
				try {
					ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.dat",false));
					out.writeObject(al);
				} catch (Exception e2) {
					// TODO: handle exception
				}

				jtxtnum.setText("");
				jtxtname.setText("");
				jtxtzip.setText("");
				jtxtadd.setText("");
				jtxttel.setText("");
				jc.setSelectedItem("");
				jtxtemail.setText("");
			}
		});
		
		/**
		 * 退出按钮
		 */
		jbtexit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					System.exit(1);
				} catch (Exception e2) {
					// TODO: handle exception
					System.out.println("退出系统出错");
				}
			}
		});
		
		/**
		 * 弹出窗口按钮
		 */
		jbtleap.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
					al=(ArrayList)in.readObject();
					in.close();
				} catch (Exception ex) {
					// TODO: handle exception
					System.out.println("添加信息时文件读取错误");
				}
				
				if(al.isEmpty()){
					JOptionPane.showMessageDialog(null,"你的通讯录空空如也，请广交好友","错误提示",JOptionPane.INFORMATION_MESSAGE);
				}
				else{
				frame2 f2=new frame2(al);
				f2.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e){
						setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
					}
				});
				f2.setSize(600,450);
				f2.setVisible(true);}
			}
		});
		
		cp.add(jbtleap);
		cp.add(title);
		cp.add(num);
		cp.add(name);
		cp.add(zip);
		cp.add(address);
		cp.add(telephone);
		cp.add(mobile);
		cp.add(email);
		cp.add(jtxtnum);
		cp.add(jtxtname);
		cp.add(jtxtzip);
		cp.add(jtxtadd);
		cp.add(jtxttel);
		cp.add(jc);
		cp.add(jtxtemail);
		cp.add(jbtadd);
		cp.add(jbtfind);
		cp.add(jbtclear);
		cp.add(jbtexit);
		cp.add(jbtedit);
	}
}